package com.frejoys.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.util.CheckUtil;
import com.frejoys.common.vo.app.CashPasswordStatusVo;
import com.frejoys.dao.entity.UserCashPassword;
import com.frejoys.dao.mapper.UserCashPasswordMapper;
import com.frejoys.service.UserCashPasswordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserCashPasswordServiceImpl extends ServiceImpl<UserCashPasswordMapper, UserCashPassword> implements UserCashPasswordService {


    @Override
    public boolean addInfo(Long userId, String password) {

        CheckUtil.password(password);

        UserCashPassword u = baseMapper.getUserCashPasswordByUserId(userId);

        if (u == null){
//            String secret = RandomUtil.randomString(6);
//            String oldPassword = SecureUtil.hmacMd5(secret).digestHex(password);
            save(new UserCashPassword(){{
                setUserId(userId);
                setPassword(password);
            }});
        }

        return true;
    }

    @Override
    public CashPasswordStatusVo cashPassword(Long userId) {

        CashPasswordStatusVo vo = new CashPasswordStatusVo();

        UserCashPassword u = baseMapper.getUserCashPasswordByUserId(userId);

        if (u != null){
            vo.setStatus(1);
        }else {
            vo.setStatus(0);
        }
        return vo;
    }


    @Override
    public boolean upInfo(Long userId, String oldPassword, String newPassword) {

        UserCashPassword u = baseMapper.getUserCashPasswordByUserId(userId);

        if (u == null){
            throw new GeneralException(HttpStatus.notCashPasswordError);
        }
//        String secret = RandomUtil.randomString(6);
//        String password = SecureUtil.hmacMd5(secret).digestHex(oldPassword);

        if (!oldPassword.equals(u.getPassword())){
            throw new GeneralException(HttpStatus.cashPasswordError);
        }

        LambdaUpdateWrapper<UserCashPassword> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCashPassword::getUserId,userId)
                .set(UserCashPassword::getPassword,newPassword);
        return update(wrapper);
    }

    @Override
    public boolean verifyCashPassword(Long userId, String password) {
        UserCashPassword u = baseMapper.getUserCashPasswordByUserId(userId);

        if (u == null){
            throw new GeneralException(HttpStatus.notCashPasswordError);
        }
//        String secret = RandomUtil.randomString(6);
//        String p = SecureUtil.hmacMd5(secret).digestHex(password);

        if (!password.equals(u.getPassword())){
            throw new GeneralException(HttpStatus.cashPasswordError);
        }

        return true;
    }
}
